<?php 
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=statistics","root","zqVnUy6cxsQXbpEZ");
$pdo->exec("set names utf8");
if (isset($_GET['stats']) && $_GET['stats']) {
    $stats = strtotime($_GET['stats']);
}
if (isset($_GET['end']) && $_GET['end']) {
    $end = strtotime($_GET['end']);
}
if (isset($_GET['sort']) && $_GET['sort']) {
    $sort = $_GET['sort'];
}
if (isset($_GET['appid']) && $_GET['appid']) {
    $appid = $_GET['appid'];
}
if (isset($_GET['app_name']) && $_GET['app_name']) {
    $app_name = $_GET['app_name'];
}
$sql = "SELECT * FROM statistics_application_info WHERE 1 = 1";
if (isset($stats)) {
    $sql .= " AND `time` > '$time'";
}
if (isset($end)) {
    $sql .= " AND `time` < '$end'";
}
if (isset($appid)) {
    $sql .= " AND application_info_id = '$appid'";
}
if (isset($app_name)) {
    $sql .= " AND application_name like '%$app_name%'";
}
//获取总数分页
$count_sql = $sql." GROUP BY application_name";
$count_data = $pdo->query($count_sql)->fetchAll(PDO::FETCH_ASSOC);
//获取数据
$application_info = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$application_data = array();
foreach ($application_info as $key => $value) {
  $application_data[$value['application_name']][] = $value;
}
$data = array();
$arr = array();
foreach ($application_data as $key => $value) {
    $install = 0;
    $uninstall = 0;
    foreach ($value as $k => $val) {
        if (empty($val['install'])) {
            $install += 1; 
        }else{
            $uninstall += 1;
        }
    $data['app_id'] = $val['application_info_id'];
    $data['app_name'] = $val['application_name'];
    $data['app_package'] = $val['package_name'];
    $data['install'] = $install;
    $data['uninstall'] = $uninstall;
    }
    $arr[] =$data;
}
 ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>应用信息统计</title>

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/datepicker3.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">
</head>
<body>
	<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
		<div class="container-fluid">
			<div class="navbar-header">
				<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#sidebar-collapse">
					<span class="sr-only">Toggle navigation</span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
					<span class="icon-bar"></span>
				</button>
				<a class="navbar-brand" href=""><span>数据统计</span>嗨镜</a>
				<ul class="user-menu">
					<li class="dropdown pull-right">
						<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-user"></span> User <span class="caret"></span></a>
						<ul class="dropdown-menu" role="menu">
							<li><a href="#"><span class="glyphicon glyphicon-log-out"></span> 退出登陆</a></li>
						</ul>
					</li>
				</ul>
			</div>
		</div><!-- /.container-fluid -->
	</nav>
		
	<div id="sidebar-collapse" class="col-sm-3 col-lg-2 sidebar">
		<ul class="nav menu">
			<li><a href="index.php"><span class="glyphicon glyphicon-dashboard"></span> 设备日活统计</a></li>
            <li class="active"><a href="application_index.php"><span class="glyphicon glyphicon-dashboard"></span> 应用日活统计</a></li>
            <li><a href="device_use_duration.php"><span class="glyphicon glyphicon-th"></span> 设备使用时长统计</a></li>
            <li><a href="application_use_duration.php"><span class="glyphicon glyphicon-th"></span> 应用使用时长统计</a></li>
            <li><a href="device_info.php"><span class="glyphicon glyphicon-stats"></span> 设备信息统计</a></li>
            <li><a href="user_info.php"><span class="glyphicon glyphicon-list-alt"></span> 用户信息统计</a></li>
            <li><a href="application_info.php"><span class="glyphicon glyphicon-pencil"></span> 应用信息统计</a></li>
            <li><a href="device_time_interval.php"><span class="glyphicon glyphicon-info-sign"></span> 设备日均时段统计</a></li>
            <li><a href="application_time_interval.php"><span class="glyphicon glyphicon-info-sign"></span> 应用日均时段统计</a></li>
		</ul>
		<div class="attribution">More Templates <a href="http://www.cssmoban.com/" target="_blank" title="模板之家">模板之家</a> - Collect from <a href="http://www.cssmoban.com/" title="网页模板" target="_blank">网页模板</a></div>
	</div><!--/.sidebar-->
		
	<div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2 main">			
		<form action="">
            <table>
                <tr>
                    <td>
                        <b>开始日期：</b><input name="start" id="start" value="<?php echo isset($start)?$start:'' ?>" type="text" style="width:120px;margin-top:30px;border:2;border-color:green;" placeholder="开始日期">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <b>结束日期：</b><input name="end" id="end" value="<?php echo isset($end)?$end:'' ?>" type="text" style="width:120px;margin-top:30px;border:2;border-color:green;" placeholder="结束日期">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <b>排序方式：</b><input name="sort" id="sort" value="<?php echo isset($sort)?$sort:'' ?>" type="text" style="width:120px;margin-top:30px;border:2;border-color:green;" placeholder="排序方式">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </td>
                    <td>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" style="width:120px;border:2;border-color:yellow;background-color:yellow;margin-top:30px;" value="搜索">  
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>应用ID：</b>&nbsp;&nbsp;&nbsp;&nbsp;<input name="appid" id="appid" value="<?php echo isset($appid)?$appid:'' ?>" type="text" style="width:120px;margin-top:30px;border:2;border-color:green;" placeholder="应用ID">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <b>应用名称：</b><input name="app_name" id="app_name" value="<?php echo isset($app_name)?$app_name:'' ?>" type="text" style="width:120px;margin-top:30px;border:2;border-color:green;" placeholder="应用包名">
                    </td>
                    <td></td>
                </tr>
            </table>
        </form>
        <br>
        <br> 
        <!-- <center>   -->
            <table border="1" style="width: 100%;height: 80%">
                <tr style="height: 30px">
                    <td align="center">应用ID</td>
                    <td align="center">应用名称</td>
                    <td align="center">应用包名</td>
                    <td align="center">下载量</td>
                    <td align="center">安装量</td>
                </tr>
                <?php foreach ($arr as $key => $value) { ?>
                    <tr style="height: 30px">
                        <td align="center"><?php echo $value['app_id'] ?></td>
                        <td align="center"><?php echo $value['app_name'] ?></td>
                        <td align="center"><?php echo $value['app_package'] ?></td>
                        <td align="center"><?php echo $value['install'] ?></td>
                        <td align="center"><?php echo $value['install']-$value['uninstall'] ?></td>
                    </tr>
                <?php } ?>
            </table>
        <!-- </center>  -->
	</div>	<!--/.main-->

	<script src="js/jquery-1.11.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<script src="js/chart.min.js"></script>
	<script src="js/chart-data.js"></script>
	<script src="js/easypiechart.js"></script>
	<script src="js/easypiechart-data.js"></script>
	<script type="text/javascript" src="./js/echarts.min.js"></script>
	<script src="js/bootstrap-datepicker.js"></script>
	<script>

	</script>	
</body>

</html>
